var data1,data2,data3,data4;
BUI.use('common/page');
BUI.use([ 'bui/chart','bui/form' ],
function(Chart,Form) {
	var form = new Form.Form({
		srcNode : '#searchForm'
	}).render();
	
	showChart();
	
	$("#btnSearch").bind('click', function() {
		showChart();
	});
});

function showChart(){
	var chartData;
	var aQuery = $("#searchForm").serializeArray();		
	$.ajax({
		url : "queryStatic",
		data : aQuery,
		type : "POST",
		async: false,
		dataType: "json",
		success : function(data){
			if(data!=null && data!=""){
				chartData = [data.custCount,data.strategic,data.pass,data.signedCount,data.newLoad]
				$("#custCount").text(data.custCount);
				$("#applyMoney").text((data.applyMoney/1000000).toFixed(2));
				$("#loanMoney").text((data.loanMoney/1000000).toFixed(2));
				
				data1 = [{'custCount':data.custCount,'infoCount':data.infoCount,'zhimafenCount':data.zhimafenCount,'mobileFullCount':data.mobileFullCount,
					'mobileFalseCount':data.mobileFalseCount,'mobileFailCount':data.mobileFailCount,'mobileCrawlingCount':data.mobileCrawlingCount},
					{'custCount':'','infoCount':percent(data.infoCount,data.custCount),'zhimafenCount':percent(data.zhimafenCount,data.custCount),'mobileFullCount':percent(data.mobileFullCount,data.custCount),
						'mobileFullCount':percent(data.mobileFullCount,data.custCount),'mobileFalseCount':percent(data.mobileFalseCount,data.custCount),'mobileFailCount':percent(data.mobileFailCount,data.custCount),'mobileCrawlingCount':percent(data.mobileCrawlingCount,data.custCount)}];
				
				var total  = data.standardCount+data.creditCardCount+data.alipayCount+data.jdCount;
				data2 = [{'standardCount':data.standardCount,'creditCardCount':data.creditCardCount,'alipayCount':data.alipayCount,'jdCount':data.jdCount},
				         {'standardCount':percent(data.standardCount,total),'creditCardCount':percent(data.creditCardCount,total),'alipayCount':percent(data.alipayCount,total),'jdCount':percent(data.jdCount,total)}];
				
	
				data3 = [{'pass':data.pass,'identityCount':data.identityCount,'cardCount':data.cardCount},
				         {'pass':'','identityCount':percent(data.identityCount,data.pass),'cardCount':percent(data.cardCount,data.pass)}];
				
				
				data4 = [{'signedCount':data.signedCount,'auditFailCount':data.auditFailCount,'auditRefuseCount':data.auditRefuseCount},
				         {'signedCount':'','auditFailCount':percent(data.auditFailCount,data.signedCount),'auditRefuseCount':percent(data.auditRefuseCount,data.signedCount)}];
				$("#Avalue").text(percent(chartData[1],chartData[0]));
				$("#Bvalue").text(percent(chartData[2],chartData[1]));
				$("#Cvalue").text(percent(chartData[3],chartData[2]));
				$("#Dvalue").text(percent(chartData[4],chartData[3]));
				$(".bui-chart").remove();
			}
		}
	});		
var xData=['注册','决策','审核通过','新用户签约','新用户放款'];

    
	var chart = new BUI.Chart.Chart({
          id : 'canvas', //render改成 id
          width : 950,
          height : 400,
          plotCfg : {
            margin : [50,50,80] //画板的边距
          },
          xAxis : {
            categories : xData
          },
          seriesOptions : { //设置多个序列共同的属性
              lineCfg : { //不同类型的图对应不同的共用属性，lineCfg,areaCfg,columnCfg等，type + Cfg 标示
                labels : { //标示显示文本
                  label : { //文本样式
   
                    y : -15
                  },
                  //渲染文本
                  renderer : function(value,item){ //通过item修改属性
                    return value;
                  }
                }
              }
            },
          tooltip : {
            valueSuffix : '',
            shared : true, //是否多个数据序列共同显示信息
            custom : true, //自定义tooltip
            html : '#tip',//自定义tooltip的 模板或者 #id
            customFollow : false, //自定义tooltip不跟随移动
            itemTpl : '<tr><td>{index}</td><td>{name}</td><td>{value}{suffix}</td></tr>',
            crosshairs : true //是否出现基准线
          },
          series : [{
            name: '信用花',
            data: chartData
          }]
        });
 
        chart.render();
}

function btnShow(flag){
	var data;
	var columns;
	var Overlay = BUI.Overlay,Grid = BUI.Grid,Data = BUI.Data;
	var Store = Data.Store;
	if("1"==flag){
		columns = [
		   	    {title : '注册',dataIndex :'custCount', width:80},
		   		{title : '完成个人信息',dataIndex :'infoCount', width:80},
		   		{title : '完成芝麻分',dataIndex : 'zhimafenCount',width:80},
		   		{title : '完成手机认证',dataIndex : 'mobileFullCount',width:80},
		   		{title : '手机未实名',dataIndex : 'mobileFalseCount',width:80},
		   		{title : '手机失败',dataIndex : 'mobileFailCount',width:80},
		   		{title : '手机获取中',dataIndex : 'mobileCrawlingCount',width:80}
		   	  ];
		data = data1;
		   	  
	}
	if("2"==flag){
		columns = [
		   	    {title : '标准',dataIndex :'standardCount', width:80},
		   		{title : '信用卡',dataIndex :'creditCardCount', width:80},
		   		{title : '支付宝',dataIndex : 'alipayCount',width:80},
		   		{title : '京东',dataIndex : 'jdCount',width:80}
		   	  ]
		data = data2
	}
	if("3"==flag){
		columns = [
		   	    {title : '审核通过',dataIndex :'pass', width:80},
		   		{title : '完成身份认证',dataIndex :'identityCount', width:80},
		   		{title : '完成绑卡',dataIndex : 'cardCount',width:80}
		   	  ]
		data = data3
	}
	if("4"==flag){
		columns = [
		   	    {title : '签约',dataIndex :'signedCount', width:80},
		   		{title : '未接通',dataIndex :'auditFailCount', width:80},
		   		{title : '电审拒绝',dataIndex :'auditRefuseCount', width:80},
		   	  ]
		data = data4
	}	
	var store = new Store({
		data : data,
	    autoLoad:true
	});
	
    grid = new Grid.Grid({
    	forceFit: true, // 列宽按百分比自适应
    	columns : columns,
    	store : store
	});
	var dialog = new Overlay.Dialog({
	    title:'统计',
	    width:(columns.length+1)*80,
	    height:150,
	    children : [grid],
	    childContainer : '.bui-stdmod-body',
	    buttons:[
         {
            text:'关闭',
            elCls : 'button',
            handler : function(){
              this.close();
            }
          }]
	  });
	
	dialog.show();
	
}

function percent(a,b){
	if(b==null || b==0){
		return 0+"%";
	}
	return (a/b*100).toFixed(0)+"%";
}
